import Vue from "vue"
import Vuex from "vuex"

Vue.use(Vuex);

const store = new Vuex.Store({
    state:{
        count:100,
        goods:[
            {id:100, name:"剃须刀",price:105.2, num:3},
            {id:101, name:"剃须刀",price:2.11, num:5},
            {id:102, name:"剃须刀",price:9.9, num:19},
            {id:103, name:"剃须刀",price:192.67, num:56},
            {id:104, name:"剃须刀",price:20.9, num:37},
            {id:105, name:"剃须刀",price:30.9, num:47},
            {id:106, name:"剃须刀",price:6.9, num:57},
            {id:107, name:"剃须刀",price:50.9, num:67},
        ],
        profile:{
            userId:"abcdef",
            pwd:"!@#$%123",
            name:"王大爷",
        }
    },
    mutations:{
        increment(state){
            state.count ++;
        },
        decrement(state){
            state.count --;
        },
        addCount(state, payLoad){
            state.count += payLoad.count;
        },
        addGood(state, payLoad){
            // state.goods.push(payLoad);
            state.goods.push(payLoad.newGood);
        },
        //同步的回调函数
        updateProfile(state, payLoad){
            state.profile.name = payLoad.newInfo.name;
            state.profile.userId = payLoad.newInfo.userId;
            state.profile.pwd = payLoad.newInfo.pwd;
            state.profile.sex = payLoad.newInfo.sex;
            //代码执行到这里的时候更新完毕
            payLoad.succeed();
        }
    },
    // 个人信息修改完之后,展示到其他子路由里面
    // 等你这边修改完之后,我在去刷新我的页面
    actions:{
        // 执行期上下文 context -> store
        aupdateProfile(context, payLoad){
            //异步操作
            setTimeout(() => {
                // context.commit("updateProfile", payLoad);
                // context.commit("updateProfile", payLoad);
                console.log(payLoad);
                context.commit("updateProfile", payLoad);
            }, 3000);
        }
    },
    getters:{
        vipGoods(state){
            // const goods = state.goods;
            // const arr = [];
            // for(let i = 0; i< goods.length; i++){
            //   if(goods[i].price > 30){
            //     arr.push(goods[i]);
            //   }
            // }
            // return arr;

            //版本2 你们以后都会越来越强
            return state.goods.filter( item => item.price > 100);
        },
        hotGoods(state, getters){
            return getters.vipGoods.filter( item => item.num > 50);
        },
        userGoods(state, getters){
            const goods = getters.vipGoods.filter( item => item.num > 50);

            return state.profile.name + JSON.stringify(goods);
        },
        // 用户自己选择喜欢的商品
        selectGoods(state, getters){
            return function(price, num){
                
                let p = price * 1;
                let n = num * 1;

                console.log(typeof p + p, typeof n + n);

                return state.goods.filter( item => {
                    return item.price > p && item.num > n;
                } );
            }
        }
    },
    modules:{
    }
});

export default store;